/**
 * Created by Administrator on 2017/7/13.
 */

// var mask=d
$(".addBook").click(function(){
	$('.mask').show();
})
$(".refresh").click(function(){
	/*$('.mask').hide();*/
	$(".tables").empty();
	$.get("http://localhost:6500/books",function(data){
		console.log(data)
        for(var v in data){
            var tr=$("<tr></tr>"),
                td1=$("<td></td>"),
                td2=$("<td></td>"),
                td3=$("<td></td>"),
                td4=$("<td></td>"),
                td5=$("<td></td>"),
                td6=$("<td></td>"),
                td7=$("<td></td>"),
                td1=$("<td></td>"),
                td1=$("<td></td>"),
                img=$("<img>"),
                input=$("<input	>");
            $(input).appendTo($(td1));//第一个是复选框
            $(input).attr("type","checkbox");
            $(td1).appendTo($(tr));
            $(img).appendTo($(td2));//图片
            $(img).attr("src",data[v].bookImages).css("width","30px");

            console.log(data[v].bookImages);
            $(td2).appendTo($(tr));
            $(td3).text(data[v].bookName).appendTo($(tr));//书名
            $(td4).text(data[v].bookAuthor).appendTo($(tr));//作者
            $(td5).text(data[v].priceVIP).appendTo($(tr));//会员价
            $(td6).text(data[v].bookPress).appendTo($(tr));//出版署
            var handleBtn=$("<button id='handle'>修改</button>"),deleteBtn=$("<button id='delete'>删除</button>")
            $(deleteBtn).appendTo($(td7));
            $(handleBtn).appendTo($(td7));
            // $(handleBtn).click(deleteHandle());
            $(deleteBtn).onclick=deleteHandle;
            $(td7).appendTo($(tr));//操作
            tr.appendTo($(".tables"));
        }
	})
})

//获取操作窗口
function addBookHandle(data){


}

// 添加数据
$(".add").click(function(){
	$(".mask").hide();
	var mask=document.querySelector(".list");
	var inputs=mask.querySelectorAll("input");
	console.log(inputs.length);
	var obj={
		bookName:$("#bookName").val(),
		bookTranslate:$("#bookTranslate").val(),
		VIP:$("#VIP").val(),
		bookPrice:$("#bookPrice").val(),
		bookAuthor:$("#bookAuthor").val(),
		bookPress:$("#bookPress").val(),
		bookGrade:$("#bookGrade").val(),
		bookBuy:$("#bookBuy").val(),
		groundTime:$("#groundTime").val(),
		priceVIP:$("#priceVIP").val(),
		ISBN:$("#ISBN").val(),
		Ebook:$("#Ebook").val(),
		PriceE:$("#PriceE").val(),
		priceV:$("#priceV").val(),
		bookEbook:$("#bookEbook").val(),
		bookImages:$("#bookImages").val()
		
	};


	$.post("http://localhost:6500/books",obj,function(data){
		console.log(data)
	});
	var obj={
		bookName:$("#bookName").val(""),
		bookTranslate:$("#bookTranslate").val(""),
		VIP:$("#VIP").val(""),
		bookPrice:$("#bookPrice").val(""),
		bookAuthor:$("#bookAuthor").val(""),
		bookPress:$("#bookPress").val(""),
		bookGrade:$("#bookGrade").val(""),
		bookBuy:$("#bookBuy").val(""),
		groundTime:$("#groundTime").val(""),
		priceVIP:$("#priceVIP").val(""),
		ISBN:$("#ISBN").val(""),
		Ebook:$("#Ebook").val(""),
		PriceE:$("#PriceE").val(""),
		priceV:$("#priceV").val(""),
		bookEbook:$("#bookEbook").val(""),
		bookImages:$("#bookImages").val("")
		
	};

})

//删除事件
function deleteHandle(){
	$.ajax({
		type:"delete",
		url:"http://localhost:6500/books/<id>",
		async:true,
		success:function(data){
			console.log(data)
		}
	})
}